package com.nanjia.boot.stock.productbatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.manufacturer.model.query.ManufacturerQuery;
import com.nanjia.boot.manufacturer.model.vo.ManufacturerVO;
import com.nanjia.boot.manufacturer.service.ManufacturerService;
import com.nanjia.boot.product.sku.model.query.ProductSkuQuery;
import com.nanjia.boot.product.sku.model.vo.ProductSkuVO;
import com.nanjia.boot.product.sku.service.ProductSkuService;
import com.nanjia.boot.product.spu.model.query.ProductSpuQuery;
import com.nanjia.boot.product.spu.model.vo.ProductSpuVO;
import com.nanjia.boot.product.spu.service.ProductSpuService;
import com.nanjia.boot.stock.productbatch.model.form.ProductBatchForm;
import com.nanjia.boot.stock.productbatch.model.query.ProductBatchQuery;
import com.nanjia.boot.stock.productbatch.model.vo.ProductBatchVO;
import com.nanjia.boot.stock.productbatch.service.ProductBatchService;
import com.nanjia.boot.warehouse.model.query.WarehouseQuery;
import com.nanjia.boot.warehouse.model.vo.WarehouseVO;
import com.nanjia.boot.warehouse.service.WarehouseService;
import com.nanjia.boot.warehouse.storagelocation.model.query.WarehouseStorageLocationQuery;
import com.nanjia.boot.warehouse.storagelocation.model.vo.WarehouseStorageLocationVO;
import com.nanjia.boot.warehouse.storagelocation.service.WarehouseStorageLocationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品批次主记录前端控制层
 *
 * @author 周洋
 * @since 2025-08-21 16:30
 */
@Tag(name = "产品批次主记录接口")
@RestController
@RequestMapping("/api/v1/product-batch")
@RequiredArgsConstructor
public class ProductBatchController {

    private final ProductBatchService productBatchService;

    private final ManufacturerService manufacturerService;

    private final ProductSkuService productSkuService;
    private final ProductSpuService productSpuService;

    private final WarehouseService warehouseService;

    private final WarehouseStorageLocationService warehouseStorageLocationService;

    @Operation(summary = "仓库列表")
    @GetMapping("/getWarehouseOptions")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
    public Result<List<WarehouseVO>> getWarehouseOptions(WarehouseQuery queryParams) {
        List<WarehouseVO> warehouseVOS = warehouseService.listWarehouseVo(queryParams);
        return Result.success(warehouseVOS);
    }

    @Operation(summary = "库位列表")
    @GetMapping("/getWarehouseStorageLocationOptions")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
    public Result<List<WarehouseStorageLocationVO>> getWarehouseStorageLocationOptions(WarehouseStorageLocationQuery queryParams) {
        Result<List<WarehouseStorageLocationVO>> result = warehouseStorageLocationService.getWarehouseStorageLocationOptions(queryParams);
        return result;
    }

    @Operation(summary = "厂家列表")
    @GetMapping("/getManufacturerOptions")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
    public Result<List<ManufacturerVO>> getManufacturerOptions(ManufacturerQuery queryParams) {
        Result<List<ManufacturerVO>> result = manufacturerService.getManufacturerList(queryParams);
        return result;
    }

//    @Operation(summary = "SPU列表")
//    @GetMapping("/getSpuOptions")
//    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
//    public Result<List<ProductSpuVO>> getSpuOptions(ProductSpuQuery queryParams) {
//        List<ProductSpuVO> productSpuVOList = productSpuService.listSpu(queryParams);
//        return Result.success(productSpuVOList);
//    }

//    @Operation(summary = "SKU列表")
//    @GetMapping("/getSkuOptions")
//    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
//    public Result<List<ProductSkuVO>> getSkuOptions(ProductSkuQuery queryParams) {
//        Result<List<ProductSkuVO>> result = productSkuService.getSkuOptions(queryParams);
//        return result;
//    }

    @Operation(summary = "产品批次主记录分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:query')")
    public PageResult<ProductBatchVO> getProductBatchPage(ProductBatchQuery queryParams) {
        IPage<ProductBatchVO> result = productBatchService.getProductBatchPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增产品批次主记录")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:add')")
    public Result<Void> saveProductBatch(@RequestBody @Valid ProductBatchForm formData, HttpServletRequest request) {
        return productBatchService.saveProductBatch(formData, IPUtils.getIpAddr(request));
//        return Result.judge(result);
    }

    @Operation(summary = "获取产品批次主记录表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:edit')")
    public Result<ProductBatchForm> getProductBatchForm(
            @Parameter(description = "产品批次主记录ID") @PathVariable Long id
    ) {
        ProductBatchForm formData = productBatchService.getProductBatchFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改产品批次主记录")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:edit')")
    public Result<Void> updateProductBatch(
            @Parameter(description = "产品批次主记录ID") @PathVariable Long id,
            @RequestBody @Validated ProductBatchForm formData
            , HttpServletRequest request) {
        boolean result = productBatchService.updateProductBatch(id, formData, IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

    @Operation(summary = "删除产品批次主记录")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('product_batch:product-batch:delete')")
    public Result<Void> deleteProductBatchs(
            @Parameter(description = "产品批次主记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productBatchService.deleteProductBatchs(ids);
        return Result.judge(result);
    }
}
